﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using D3;
using D3.Pathing;
using KCBF.Modules.AI;
using KCBF.Modules.AI.QuestSteps;
using KCBF.Modules.AI.TreeSharp;
using NYTBD.Modules;
using NYTBD.Common;
using YAMB.Common;
using Action = KCBF.Modules.AI.TreeSharp.Action;


namespace NYTBD.Bots
{
    class AShatteredCrown : IBot
    {
        private Logger _logger;
        private int _tickCount = 0;
        //private Behavior _tree;
        private GroupComposite _myBehavior;

        public AShatteredCrown()
        {

            Multiverse.ToogleMapDraw();
                      
            /*_tree.AddChild(new Move(2947, 2792));
            _tree.AddChild(new Interact("Deckard Cain"));
            _tree.AddChild(new Move(2938, 2615));
            _tree.AddChild(new Interact("Haedrig"));
            _tree.AddChild(new Move(2794, 2620));
            _tree.AddChild(new WaitForUnit("Cellar of the Damned"));
            _tree.AddChild(new Interact("Cellar of the Damned"));
            _tree.AddChild(new Move(170, 152));
            _tree.AddChild(new Destroy("Sturdy"));
            _tree.AddChild(new Move(118, 149));
            _tree.AddChild(new KillAll(200));
            _tree.AddChild(new Move(73, 146));
            _tree.AddChild(new WaitHostile("{c:ffc7b377}"));
            _tree.AddChild(new Kill("{c:ffc7b377}"));
            _tree.AddChild(new Loot(200));
            _tree.AddChild(new Move("Haedrig"));
            _tree.AddChild(new Interact("Haedrig"));
            _tree.AddChild(new GoTown());
            _tree.AddChild(new Town());*/


            _myBehavior = new PrioritySelector();
            _myBehavior.AddChild(new TalkToNpc("Deckard Cain", 2947, 2792));
            _myBehavior.AddChild(new TalkToNpc("Haedrig",2938,2615));
            _myBehavior.AddChild(new OperateEnvironment("Cellar of the Damned",2794,2620));
            _myBehavior.AddChild(new DestroyEnvironment("Sturdy", 180, 152));


            Game.OnDrawEvent += new DrawEventHandler(Game_OnDrawEvent);
            Game.OnTickEvent += new TickEventHandler(Game_OnTickEvent);
        }

        public void Reset()
        {
            //_tree = new Sequence();
            //_tree.AddChild(new Move(2947, 2792));
        }

        public void Game_OnTickEvent(EventArgs e)
        {

        }

        static void Game_OnDrawEvent(EventArgs e)
        {
            if (Game.Ingame)
            {
                Draw.DrawText(10.0f, 10.0f, 0x16A, 0x16, 0xFFFFFFFF, String.Format("Me Pos: {0}, {1}", Me.X, Me.Y));
            }
        }

        public Quests GetQuest()
        {
            return Quests.A1_A_Shattered_Crown;
        }

        public int GetDifficulty()
        {
            return 1;
        }

        public int GetQuestStep()
        {
            return 0;
        }



        public void Execute()
        {

            Thread.Sleep(2000);
         
            var q = (Queue<Point>)Blackboard.GetGlobal(MoveToAction.NextMoveProperty);
            q.Enqueue(new Point(2947, 2792));
            q.Enqueue(new Point(2938, 2615));


            
            object context = new object();
            
            _myBehavior.Start(context);
            var run = _myBehavior.Tick(context);

            while (true)
            {
                RunStatus s = _myBehavior.Tick(context);
                if (s == RunStatus.Success) _myBehavior.Start(context);
                if (s == RunStatus.Failure) break;
                Thread.Sleep(20);
            }

            Game.Print("Were done");

        }
    }
}
